package com.xiaotu.spider.qh360.pageprocessor;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.TVQiHuExponentialModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

/**
 * @类名 TVQiHuPageProcessor
 * @日期 2017年12月20日
 * @作者 王艳龙
 * @功能 作品奇虎数据爬取
 */
@Component("TVQiHuPageProcessor")
@Scope("prototype")
public class TVQiHuPageProcessor extends APageProcessor
{
	//奇虎360 指数首页
    protected static final String qiHusearchUrl = "https://trends.so.com/result?query=";
    //生成环境更换成   /data/index/
	private final static String savePath = PropertiesUtil.getProperty("exponential.save.path");
	
    @Override
    protected Request getStartRequest(JSONObject target)
    {
        return new Request(qiHusearchUrl + KeywordEncode.encode(target.getString(this
                .getNameField()))+"&period=30");
    }

    protected String getNameField()
    {
        return "tvName";
    }

    protected String getIdField()
    {
        return "tvid";
    }

    protected String getTargetType()
    {
        return Constants.TargetType.TV;
    }

    @Override
    protected String getSpiderType()
    {
    	return "TVQiHu";
    }

    @Override
    public String getPageDataType(Page page)
    {
        String url = page.getUrl().toString();
 		if (url.contains("https://trends.so.com"))
 			return Constants.PageDataType.SEARCH_LIST;
 		else if (url.contains("https://index.so.com/index.php"))
 			return Constants.PageDataType.TV_RECORD;
 		return Constants.PageDataType.UNKNOW;
    }

    @Override
    public void process(Page page)
    {
    	Map<String, Object> tvInfoMap = page.getRequest().getExtras();
        String dataType = this.getPageDataType(page);
        switch (dataType)
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.searchListProcess(page, tvInfoMap);
                break;
            case Constants.PageDataType.TV_RECORD:
                this.dataProcess(page, tvInfoMap);
                break;
            default:
                break;
        }
    }

    private void searchListProcess(Page page,final Map<String, Object> tvInfoMap){
    	String tvName =KeywordEncode.encode(page.getRequest().getExtra(this.getNameField()).toString());
    	//String tvName =KeywordEncode.encode((String)tvInfoMap.get("name"));
    	//360  人群属性
    	String qh_renqunshuxing ="https://index.so.com/index.php?a=portrayalJson&t=30&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_renqunshuxing,tvInfoMap);
    	//360 地域分布   
    	String qh_diyu ="https://index.so.com/index.php?a=drawAreaJson&t=30&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_diyu,tvInfoMap);
    	//需求分布：
    	String qh_xuqiu ="https://index.so.com/index.php?a=radarJson&t=30&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_xuqiu,tvInfoMap);
    	//相关搜索趋势：
    	String qh_xiangguanqushi ="https://index.so.com/index.php?a=nlpJson&t=30&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_xiangguanqushi,tvInfoMap);
    	//相关新闻：
    	String qh_xiangguanxinwen ="https://index.so.com/index.php?a=relNewsJson&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_xiangguanxinwen,tvInfoMap);
    	//相关微博：
    	String qh_xiangguanweibo ="https://index.so.com/index.php?a=relWeiboJson&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_xiangguanweibo,tvInfoMap);
    	//热搜飙升词：
    	String qh_resoubiaoshengci ="https://index.so.com/index.php?a=surgeWordsJson&t=7&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_resoubiaoshengci,tvInfoMap);
    	//360 指数
    	String qh_zhishu ="https://index.so.com/index.php?a=overviewJson&area=%E5%85%A8%E5%9B%BD&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_zhishu,tvInfoMap);
    	//搜索指数趋势
    	String qh_searchindex ="https://index.so.com/index.php?a=soIndexJson&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_searchindex,tvInfoMap);
    	//媒体关注度
    	String qh_mediacoverage ="https://index.so.com/index.php?a=soMediaJson&q="+tvName;
    	PageUtils.addTargetRequest(page,qh_mediacoverage,tvInfoMap);
    }
    
    Map<String, Object> resultMap = new HashMap<String,Object>();
    private void dataProcess(Page page, Map<String, Object> tvMap)
    {
    	String url =page.getUrl().toString();
    	if(url.contains("a=portrayalJson")){
    		resultMap.put("qh_renqunshuxing", page.getRawText());
    	}
    	if(url.contains("a=drawAreaJson")){
    		String qh_diyu =page.getRawText();
    		if(qh_diyu.contains("<!DOCTYPE html>")){
    			qh_diyu = qh_diyu.substring(0, qh_diyu.indexOf("<!DOCTYPE html>"));
    		}
    		resultMap.put("qh_diyu", qh_diyu);
    	}
    	if(url.contains("a=radarJson")){
    		resultMap.put("qh_xuqiu", page.getRawText());
    	}
    	if(url.contains("a=nlpJson")){
    		String qh_xiangguanqushi = page.getRawText();
    		if(qh_xiangguanqushi.contains("<!DOCTYPE html>")){
    			qh_xiangguanqushi = qh_xiangguanqushi.substring(0, qh_xiangguanqushi.indexOf("<!DOCTYPE html>"));
    		}
    		resultMap.put("qh_xiangguanqushi", qh_xiangguanqushi);
    	}
    	if(url.contains("a=relNewsJson")){
    		String qh_xiangguanxinwen = page.getRawText();
    		if(qh_xiangguanxinwen.contains("<!DOCTYPE html>")){
    			qh_xiangguanxinwen = qh_xiangguanxinwen.substring(0, qh_xiangguanxinwen.indexOf("<!DOCTYPE html>"));
    		}
    		resultMap.put("qh_xiangguanxinwen", qh_xiangguanxinwen);
    	}
    	if(url.contains("a=relWeiboJson")){
    		String qh_xiangguanweibo = page.getRawText();
    		if(qh_xiangguanweibo.contains("<!DOCTYPE html>")){
    			qh_xiangguanweibo = qh_xiangguanweibo.substring(0, qh_xiangguanweibo.indexOf("<!DOCTYPE html>"));
    		}
    		resultMap.put("qh_xiangguanweibo", qh_xiangguanweibo);
    	}
    	if(url.contains("a=surgeWordsJson")){
    		resultMap.put("qh_resoubiaoshengci", page.getRawText());
    	}
    	if(url.contains("a=overviewJson")){
    		resultMap.put("qh_zhishu", page.getRawText());
    	}
    	if(url.contains("a=soIndexJson")){
    		resultMap.put("qh_searchindex", page.getRawText());
    	}
    	if(url.contains("a=soMediaJson")){
    		resultMap.put("qh_mediacoverage", page.getRawText());
    	}
    	
    	if(resultMap.size()>=10){
			page.putField(TVQiHuExponentialModel.class.getName(), new TVQiHuExponentialModel(
				page.getRequest().getExtra(this.getIdField()).toString(), 
				page.getRequest().getExtra(this.getNameField()).toString(), 
				"qh_renqunshuxing="+(String)resultMap.get("qh_renqunshuxing"), 
				"qh_diyu="+(String)resultMap.get("qh_diyu"),"qh_xuqiu="+(String)resultMap.get("qh_xuqiu"), 
				"qh_xiangguanqushi="+(String)resultMap.get("qh_xiangguanqushi"),"qh_xiangguanxinwen="+(String)resultMap.get("qh_xiangguanxinwen"), 
				"qh_xiangguanweibo="+(String)resultMap.get("qh_xiangguanweibo"),"qh_resoubiaoshengci="+(String)resultMap.get("qh_resoubiaoshengci"),
				"qh_zhishu="+(String)resultMap.get("qh_zhishu"),"qh_searchindex="+(String)resultMap.get("qh_searchindex"), 
				"qh_mediacoverage="+(String)resultMap.get("qh_mediacoverage")));	
			//生成环境更换成   /data/index/360
			page.putField("path",savePath+"360");
    	}
    }
}
